// 轮播图

class Banner {
    constructor(ele) {
        this.oParent = $(ele);

        this.index = 0;
        this.t = null;

        this.flag = true;
        this.oNext = this.oParent.find('.next');
        this.oPrev = this.oParent.find('.prev');
        this.oUl = this.oParent.find('ul');

        // 图片
        this.oLis1 = this.oParent.find('ul li');

        // 小圆点
        this.oLis2 = this.oParent.find('ol li');

        this.width = $(this.oLis1[0]).width();


    }







    autoPlay() {
        this.t = setInterval(() => {
            this.index++;
            this.showOne()
        }, 4000)
    }

    showOne() {
        this.flag = false;
        this.oUl.animate({
            left: - this.index * this.width
        }, () => {
            this.flag = true;
            if (this.index >= this.oLis1.length - 1) {
                this.index = 0;
                this.oUl.css('left', '0');
            }
        })

        if (this.index >= this.oLis1.length - 1) {
            $(this.oLis2).removeClass('on').eq(0).addClass('on');
        } else {
            $(this.oLis2).removeClass('on').eq(this.index).addClass('on');
        }

    }

    init() {

        const that = this;
        this.oParent.on('click', 'ol li', function () {

            if (that.flag) {
                clearInterval(that.t);
                that.index = $(this).index();
                that.showOne()
                that.autoPlay()
            }
        })

        this.oParent.on('click', '.prev', () => {

            if (this.flag) {
                clearInterval(this.t)
                if (this.index === 0) {
                    this.index = this.oLis1.length - 1;
                    this.oLis1.css('left', -this.index * this.width)
                }

                this.index--;
                this.showOne();
                this.autoPlay()
            }
        })

        this.oParent.on('click', '.next', () => {

            if (this.flag) {
                clearInterval(this.t)
                if (this.index === this.oLis1.length - 1) {
                    this.index = 0;
                    this.oLis1.css('left', -this.index * this.width)
                }

                this.index++;
                this.showOne()
                this.autoPlay()
            }
        })
    }

}








// 返回顶部

function retrun_top() {
    var root = document.documentElement || document.body;
    var oA = document.getElementById('goback');

    window.onscroll = function () {

        if (root.scrollTop >= 200) {
            oA.style.opacity = 1;
        } else {
            oA.style.opacity = 0;
        }

    }

    var flag = true;
    oA.onclick = function () {
        if (flag) {
            // 开始做事情之后 点击事件要失效  flag = false ;
            flag = false;
            var t = setInterval(function () {
                root.scrollTop -= 100;
                if (root.scrollTop <= 0) {
                    clearInterval(t);
                    // 事情做完以后  就可以恢复继续点击 flag = true
                    flag = true;
                }
            }, 1)
        }
    }

}



// 公告栏

function ScrollImgLeft() {
    var speed = 50;
    var MyMar = null;
    var scroll_begin = document.getElementById("scroll_begin");
    var scroll_end = document.getElementById("scroll_end");
    var scroll_div = document.getElementById("scroll_div");
    scroll_end.innerHTML = scroll_begin.innerHTML;
    function Marquee() {
        if (scroll_end.offsetWidth - scroll_div.scrollLeft <= 0)
            scroll_div.scrollLeft -= scroll_begin.offsetWidth;
        else
            scroll_div.scrollLeft++;
    }

    MyMar = setInterval(Marquee, speed);

    scroll_div.onmouseover = function () {
        clearInterval(MyMar);
    }
    scroll_div.onmouseout = function () {
        MyMar = setInterval(Marquee, speed);
    }
}


// 滑动验证

(function ($) {
    $.fn.drag = function (options) {
        var x, drag = this, isMove = false, defaults = {
        };
        var options = $.extend(defaults, options);
        //添加背景，文字，滑块
        var html = '<div class="drag_bg"></div>' +
            '<div class="drag_text" onselectstart="return false;" unselectable="on">向右拖动滑块</div>' +
            '<div class="handler handler_bg"></div>';
        this.append(html);

        var handler = drag.find('.handler');
        var drag_bg = drag.find('.drag_bg');
        var text = drag.find('.drag_text');
        var maxWidth = drag.width() - handler.width();  //能滑动的最大间距

        //鼠标按下时候的x轴的位置
        handler.mousedown(function (e) {
            isMove = true;
            x = e.pageX - parseInt(handler.css('left'), 10);
        });

        //鼠标指针在上下文移动时，移动距离大于0小于最大间距，滑块x轴位置等于鼠标移动距离
        $(document).mousemove(function (e) {
            var _x = e.pageX - x;
            if (isMove) {
                if (_x > 0 && _x <= maxWidth) {
                    handler.css({ 'left': _x });
                    drag_bg.css({ 'width': _x });
                } else if (maxWidth - _x <= 2) {  //鼠标指针移动距离达到最大时清空事件
                    dragOk();
                    return 5667

                }
            }
        }).mouseup(function (e) {
            isMove = false;
            var _x = e.pageX - x;
            if (_x < maxWidth) { //鼠标松开时，如果没有达到最大距离位置，滑块就返回初始位置
                handler.css({ 'left': 0 });
                drag_bg.css({ 'width': 0 });
            }
        });

        //清空事件
        function dragOk() {
            handler.removeClass('handler_bg').addClass('handler_ok_bg');
            text.text('检测成功,可注册');
            drag.css({ 'color': '#fff' });
            drag_bg.css({ 'background-color': '#3c3' })
            handler.unbind('mousedown');
            $(document).unbind('mousemove');
            $(document).unbind('mouseup');

        }

    };
})(jQuery);


// 顶部弹窗
window.xcsoft = window.xcsoft || {};
xcsoft.tipsCss = {
    height: "44px",
    fontSize: "16px"
};
xcsoft.tipsHide = xcsoft.tipsShow = "fast";
xcsoft.dom;
xcsoft.timeout;
xcsoft.loading = function (b) {
    xcsoft.init(b, 0, "loading", !0)
};
xcsoft.info = function (b, c) {
    xcsoft.init(b, c || 2500, "info")
};
xcsoft.error = function (b, c) {
    xcsoft.init(b, c || 2E3, "error")
};
xcsoft.success = function (b, c) {
    xcsoft.init(b, c || 1500, "success")
};
xcsoft.init = function (b, c, a, d) {
    this.tipsHtml(b, a);
    $(this.dom).animate({
        top: 0
    }, this.tipsHide);
    clearTimeout(this.timeout);
    this.timeout = !d && setTimeout(function () {
        xcsoft._hide()
    }, c)
};
xcsoft._hide = function () {
    this.dom.stop().animate({
        top: "-" + xcsoft.tipsCss.height
    }, this.tipsHide, "", function () {
        $(this).remove()
    })
};
xcsoft.tipsHtml = function (b, c) {
    var a = $(".xctips ");
    c = c || "info";
    0 == a.length ? (a = document.createElement("div"), a.className = "xctips " + c, this.dom = $(a), this.dom.css(this.tipsCss), a.style.top = "-" + this.tipsCss.height, a.style.height = this.tipsCss.height, a.style.lineHeight = this.tipsCss.height, a.innerHTML = b, $("body").append(this.dom)) : (a.html(b), a.attr("class", "xctips " + c), this.dom = a)
};




// 数字 小写 大写
var numArr = [];
for (var i = 0; i < 10; i++) {
    numArr.push(i + '')
}

var smallArr = [];
for (var i = 97; i <= 122; i++) {
    var str = String.fromCharCode(i);
    smallArr.push(str)
}

var bigArr = [];
for (var i = 65; i <= 90; i++) {
    var str = String.fromCharCode(i);
    bigArr.push(str)
}

var allArr = smallArr.concat(numArr, bigArr)

// 随机数
function rand(min, max) {
    return parseInt(Math.random() * (max - min)) + min;
}


// 随机验证码
function randomCode(n) {
    n = n || 4;
    var res = '';
    res += numArr[rand(0, numArr.length)];
    res += smallArr[rand(0, smallArr.length)];
    res += bigArr[rand(0, bigArr.length)];
    for (var i = 0; i < n - 3; i++) {
        res += allArr[rand(0, allArr.length)];
    }

    // 字符串无法交换顺序
    var arr = res.split('');
    // 交换次数至少n次，
    for (var i = 0; i < arr.length; i++) {
        // 随机取一个值，与第i个进行交换
        var index = rand(0, arr.length);
        var temp = arr[i];
        arr[i] = arr[index];
        arr[index] = temp;
    }
    return arr.join('')
}




// ajax
const ajax = options => {

    // 解构
    const {
        type,
        path,
        data = {},
        dataType = 'json',
        successCB
    } = options;

    // 改变参数的形式
    let params = '';
    // 把对象改成字符串  ? 
    for (let key in data) {
        params += `${key}=${data[key]}&`;
    }

    // 尾巴多了一个&
    params = params.substring(0, params.length - 1);

    // 做请求
    const xhr = new XMLHttpRequest();

    // 分类型
    if (type.toLowerCase() === 'get') {
        xhr.open('get', path + '?' + params, true);
        xhr.send();
    }
    else if (type.toLowerCase() === 'post') {
        xhr.open('post', path, true);
        // 设置数据格式
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        xhr.send(params);
    }

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let res = xhr.responseText;
            if (dataType.toLowerCase() === 'json') {
                res = JSON.parse(res)
            }
            successCB(res);
        }
    }
}


// 双范围滑块








